<?php

namespace App\Admin\Controllers\Order;

use App\Admin\Actions\Grid\Row\SwtOrder\CancelOrder;
use App\Admin\Actions\Grid\Row\SwtOrder\NotifyOrder;
use App\Admin\Actions\Grid\Row\SwtOrder\RefundOrder;
use App\Admin\Forms\PayOrderForm;
use App\Models\SwtOrder;
use App\Admin\Views\SwtOrder\OrderGoodsView;
use App\Enums\OrderEnum;
use App\Enums\SourceTypeEnum;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Widgets\Card;

class SwtOrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(SwtOrder::with(['dhe_order', 'machine', 'user']), function (Grid $grid) {
            $grid->model()->orderBy('id', 'desc');

            $grid->column('id')->sortable();

            $grid->column('user.name','用户名')->display(function ($value) {
                return $value ?? '-';
            });;

            $grid->column('machine.machine_name','设备名称');

            $grid->column('machine.source_type','设备厂家') -> display( function ($value) {
                return SourceTypeEnum::getSourceTypeListDesc($value);
            });

            $grid->column('order_sn')->copyable();
            $grid->column('pay_type','支付方式')->using(['支付分','小程序','会员卡'])->label(['success','danger','warning']);
            $grid->column('platform','订单来源')->using(['wechat' => '微信', 'alipay' =>'支付宝', 'card' => '会员卡'])->label(['wechat'=>'success','alipay'=>'info', 'card' => 'warning']);
            $grid->column('order_amount')->display(function ($value) {
                return $value / 100 ?: '-';
            });

            $grid->column('pay_amount')->display(function ($value) {
                return $value / 100 ?: '-';
            });

            $grid->column('further_pay_money')->display(function ($value) {
                return $value / 100 ?: '-';
            });

            $grid->column('refund_amount')->display(function ($value) {
                return $value / 100 ?: '-';
            });

            $grid->column('state') -> display(function($value) {
                if($this->is_warning == 1) {
                    return '<text class="text-warning">异常订单</text>';
                }

                return OrderEnum::getOrderStateView($value);
            });

            $grid->column('pay_state','支付状态') -> display(function() {
                if($this->need_further_pay == 1)  return '<text class="text-warning">部分支付</text>';
                if($this->is_refund == 1)  return '<text class="text-danger">已退款</text>';
                if($this->refund_amount > 0 && $this->refund_amount !== $this->pay_amount)  return '<text class="text-danger">部分退款</text>';
                if($this->is_pay == 0) return '<text class="text-info">未支付</text>';
                return '<text class="text-success">已支付</text>';
            });

            $grid->column('machine.source_type','开门记录') -> display(function($value) {
                if($value == 2) {
                    $url = admin_url('bj/swt_bj_open?order_id='.$this->id);
                    return '<a href="'.$url.'">查看</a>';
                }

                return '-';
            });

            $grid->column('created_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->like('order_sn');
            });


            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if($actions -> row -> user_id == 2) {
                    return;
                }

                $actions->disableView(false);

                //已创建和进行中订单可取消
                if(in_array($actions -> row -> state,[OrderEnum::ORDER_CREATED, OrderEnum::ORDER_DOING])) {
                    $actions->append(new CancelOrder());
                }

                if($actions -> row -> is_notify == 0 && $actions -> row -> is_pay == 1 && $actions->row->machine->source_type == 1) {
                    $actions->append(new NotifyOrder());
                }

                if($actions -> row -> is_pay == 1 && $actions -> row -> is_refund == 0) {
                    $actions->append(new RefundOrder());
                }

            });
            $grid->quickSearch('order_sn');
        });
    }

    public function show($id, Content $content)
    {
        return $content
            ->title($this->title())
            ->description('订单详情')
            ->body(function (Row $row) use ($id){
                $row->column(6, $this->orderDetail($id));
                $row->column(6, $this->orderGoods($id));
            });
    }


    public function orderDetail($id) {
        $view = new OrderGoodsView(['id'=>$id]);

        return $view -> render();
    }

    public function orderGoods($id)
    {
        $order_info = SwtOrder::find($id);

        if($order_info -> is_warning == 1) {
            $form = PayOrderForm::make()->payload(['id' => $id]) -> render();
        } else {
            $form = SwtOrderGoodController::grid($id);
        }

        $card = Card::make('订单商品', $form);

        return $card -> render();
    }
}
